const newLocal='id'
<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <h2 style="margin: 0 0 10px 0">宠泰泰（{{ total }}人）</h2>
      <!-- 搜索区 -->
      <el-form ref="searchFormRef" :inline="true" :model="queryInfo.query">
        <el-form-item label="注册时间" prop="register_time">
          <el-date-picker
            v-model="queryInfo.query.register_time"
            type="daterange"
            size="mini"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            :default-time="['00:00:00', '23:59:59']"
            class="datePickerWidth"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryInfo.query.status"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option key="0" label="启用" value="0"> </el-option>
            <el-option key="1" label="禁用" value="1"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键字" prop="keywords">
          <el-input
            v-model="queryInfo.query.keywords"
            size="mini"
            placeholder="请输入用户账号或手机号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 用户列表区域 -->
      <el-table :data="userList" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="用户账号" prop="ct_user_id"></el-table-column>
        <el-table-column label="绑定手机账号" prop="phone"></el-table-column>
        <el-table-column label="昵称" prop="nickname"></el-table-column>
        <el-table-column
          label="绑定微信账号"
          prop="wechant_account"
        ></el-table-column>
        <el-table-column label="所在城市">
          <template slot-scope="scope">
            {{ scope.row.province + scope.row.city }}
          </template>
        </el-table-column>
        <el-table-column
          label="来源平台"
          prop="platform.name"
        ></el-table-column>
        <el-table-column label="注册时间" prop="create_time"></el-table-column>
        <el-table-column
          label="最近一次登录时间"
          prop="last_enter_time"
        ></el-table-column>
        <el-table-column label="状态" prop="status" width="60px">
          <template slot-scope="scope">
            <span v-show="scope.row.status == 1">启用</span>
            <span v-show="scope.row.status == 2">禁用</span>
            <span v-show="scope.row.status == 3">注销</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="110px">
          <template slot-scope="scope">
            <el-link
              type="danger"
              v-if="scope.row.status === 1"
              @click="disable(scope.row.ct_user_id)"
            >
              禁用
            </el-link>
            <el-link
              type="primary"
              v-if="scope.row.status === 0"
              @click="enable(scope.row.ct_user_id)"
            >
              启用
            </el-link>
            <el-link type="primary" @click="showUpdateMobileDialog(scope.row)">
              修改手机号
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 修改手机号的对话框 -->
      <el-dialog
        title="修改手机号"
        :visible.sync="updateMobileDialogVisible"
        width="30%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="updateMobileDialogClosed"
      >
        <!-- 表单 -->
        <el-form
          :model="updateMobileForm"
          :rules="updateMobileFormRules"
          ref="updateMobileFormRef"
          label-width="110px"
        >
          <el-form-item label="原绑定手机号">
            <span>{{ oldMobile }}</span>
          </el-form-item>
          <el-form-item label="新绑定手机号" prop="phone">
            <el-input
              v-model="updateMobileForm.phone"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-form>
        <!-- 底部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="updateMobileDialogVisible = false"
            >取 消</el-button
          >
          <el-button class="theme-background-color" @click="updateMobile">
            确 定
          </el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var checkphone = (rule, value, callback) => {
      const phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else if (!phoneReg.test(value)) {
        // 引入methods中封装的检查手机格式的方法
        callback(new Error('请输入正确的手机号!'))
      } else if (parseInt(value) === parseInt(this.oldMobile)) {
        callback(new Error('请输入新的手机号!'))
      } else {
        callback()
      }
    }
    return {
      // 日期范围的快捷选项
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      // 获取平台用户列表的参数对象
      queryInfo: {
        query: {
          register_time: [],
          status: '',
          keywords: ''
        },
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 20
      },
      userList: [],
      total: 0,
      defaultDate: '',
      //   控制修改手机号对话框的显示与隐藏
      updateMobileDialogVisible: false,
      oldMobile: '',
      updateMobileForm: {
        uid: '',
        phone: '',
        origin_phone: ''
      },
      updateMobileFormRules: {
        phone: [{ required: true, validator: checkphone, trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getListData()
    this.queryInfo.query.register_time = this.getPastMonth()
    // console.log(this.queryInfo.query.registerTime)
  },
  methods: {
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        'admin/customer/list',
        this.queryInfo
      )
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取用户列表失败！')
      }
      this.userList = res.data.list
      this.total = res.data.total
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    async disable(id) {
      // 弹框询问是否禁用用户
      const confirmResult = await this.$confirm(
        '禁用后将无法登陆及使用系统，是否确认禁用该用户？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消操作')
      }
      // const { data: res } = await this.$http.post(
      //   'removeProductById', {
      //   idi : idf
      // }
      // )
      const { data: res } = await this.$http.post('admin/customer/active', {
        // 当前的页数
        uid: id
      })
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
        return
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    },
    async enable(id) {
      // 弹框询问是否启用用户
      const confirmResult = await this.$confirm(
        '是否确认启用该用户？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消操作')
      }
      const { data: res } = await this.$http.post('admin/customer/active', {
        // 当前的页数
        uid: id
      })
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
        return
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    },
    showUpdateMobileDialog(row) {
      this.updateMobileForm.uid = row.ct_user_id
      this.updateMobileForm.origin_phone = row.phone
      this.oldMobile = row.phone
      this.updateMobileDialogVisible = true
    },
    getPastMonth() {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      const startDate = this.getFormatDate(start)
      const endDate = this.getFormatDate(end)
      return [startDate, endDate]
    },
    updateMobile() {
      this.$refs.updateMobileFormRef.validate(async (valid) => {
        if (!valid) return
        // 新增的请求
        // const { data: res } = await this.$http.post(
        //   'addProduct',
        //   this.addForm
        // )
        // 修改的请求
        // const { data: res } = await this.$http.put(
        //   'addProduct/' + this.$route.query.id,
        //   this.addForm
        // )
        console.log(this.updateMobileForm)
        const { data: res } = await this.$http.post(
          'admin/customer/modeifyPhohe',
          this.updateMobileForm
        )
        if (res.meta.status !== 200) {
          this.$message.error(res.meta.msg)
          return
        }
        this.$message.success(res.meta.msg)
        this.updateMobileForm.phone = ''
        this.getListData()
        this.updateMobileDialogVisible = false
      })
    },
    // 监听修改手机号对话框的关闭事件
    updateMobileDialogClosed() {
      this.phone = ''
    }
  }
}
</script>

<style lang="less" scoped>
.datePickerWidth {
  width: 210px !important;
}
.el-form-item {
  margin-bottom: 0px;
}
</style>
